package com.xinlu.user.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.EmailException;
import org.apache.struts2.ServletActionContext;

import com.xinlu.common.log.Logger;
import com.xinlu.derek.common.BaseAction;
import com.xinlu.derek.common.BaseForm;
import com.xinlu.persistence.Users;
import com.xinlu.user.bo.UsersBo;
import com.xinlu.user.dao.MD5Util;
import com.xinlu.user.form.ModifyUserPwdForm;
import com.xinlu.util.tool.CheckUtils;
import com.xinlu.util.tool.EmailUtil;

public class ModifyUserPwdAction extends BaseAction{
	
	Logger log = Logger.getLogger(ModifyUserPwdAction.class);
	
	private ModifyUserPwdForm fr = new ModifyUserPwdForm();
	
	private UsersBo usersBo;
	

	public void setUsersBo(UsersBo usersBo) {
		this.usersBo = usersBo;
	}

	@Override
	public String doExecute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void formInit() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BaseForm getModel() {
		// TODO Auto-generated method stub
		return fr;
	}
	
	
	public String checkUserExist() throws IOException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		
		response.setCharacterEncoding("UTF-8");
		
		Users users = usersBo.loadUser(fr.getModifyUsername());
		
		if (users == null) {
			response.getWriter().write("fail");
		}else if (StringUtils.isEmpty(users.getEmail()) || !CheckUtils.checkEmail(users.getEmail())) {
			
			response.getWriter().write("email");
		}
		
		
		return null;
	}
	
	public String getUserDymaicPwd() throws EmailException{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		Users users = usersBo.loadUser(fr.getModifyUsername());
		
		String email = users.getEmail();
		
		String newPwd = MD5Util.encodePassword(users.getUsername() + (int)(Math.random() * 10) + (int)(Math.random() * 10) + (int)(Math.random() * 10));
		
		users.setUserpass(newPwd);
		
		usersBo.update(users);
		
		request.getSession().setAttribute("user", users);
		
		EmailUtil.sendEmailMessage(newPwd, email);
		
		
		return "wjmmMiddle";
	}
	
	public String checkOldPwd() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		
		response.setCharacterEncoding("UTF-8");
		
		Users users = usersBo.loadUser(fr.getModifyUsername());
		
		response.getWriter().write("" + (users.getUserpass().equals(fr.getOldPwd())));
		
		return null;
	}
	
	public String modifyUserNewPwd(){
		Users users = usersBo.loadUser(fr.getModifyUsername());
		usersBo.Xgdlmm(users, fr.getNewPwd());
		return null;
	}
	
	public String checkDymaicPwd() throws IOException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		
		response.setCharacterEncoding("UTF-8");
		
		Users users = usersBo.loadUser(fr.getModifyUsername());
		
		if (!fr.getOldPwd().equals(users.getUserpass())) {
			
			response.getWriter().write("fail");
		}else{
			
			usersBo.Xgdlmm(users, fr.getNewPwd());
		}
		
		
		return null;
	}
	
	
	public String sendEmailAgin()  throws IOException, EmailException{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		Users u = (Users) request.getSession().getAttribute("user");
		response.setCharacterEncoding("UTF-8");
		
		if (u != null) {
			
			Users users = usersBo.loadUser(u.getUsername());
			
			String email = users.getEmail();
			
			String newPwd = MD5Util.encodePassword(users.getUsername() + (int)(Math.random() * 10) + (int)(Math.random() * 10) + (int)(Math.random() * 10));
			
			users.setUserpass(newPwd);
			
			usersBo.update(users);
			
			request.getSession().setAttribute("user", users);
			
			EmailUtil.sendEmailMessage(newPwd, email);
		}else{
			
			response.getWriter().write("fail");
		}
		
		
		return null;
	}
	
}
